package com.gjp.myapplication;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gjp.myapplication.adapter.PhotoAdapter;
import com.gjp.myapplication.bean.ImageItem;
import com.king.image.imageviewer.ImageViewer;
import com.king.image.imageviewer.loader.GlideImageLoader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PhotoList3Activity extends AppCompatActivity {

    private List<String> listData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_list);
        init();
    }

    private void init() {
        // 获取传值
        ImageItem[] imageItems = (ImageItem[]) getIntent().getSerializableExtra("data");
        setTitle("图片列表，共" + imageItems.length + "张图片");
        // 解析出所有图片链接
        listData = new ArrayList<>();
        for (ImageItem imageItem : imageItems) {
            //清空名称说明
            imageItem.setName("");
            listData.add(imageItem.getUrl());
        }

        PhotoAdapter adapter = new PhotoAdapter(Arrays.asList(imageItems));
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(PhotoList3Activity.this, 2));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new PhotoAdapter.OnItemClickListener() {
            @Override
            public void onClick(View v, int position) {
                //图片查看器
                //data 可以多张图片List或单张图片，支持的类型可以是{@link Uri}, {@code url}, {@code path},{@link File}, {@link DrawableRes resId}…等
                ImageViewer.load(listData)//要加载的图片数据，单张或多张
                        .selection(position)//当前选中位置
                        .indicator(true)//是否显示指示器，默认不显示
                        .imageLoader(new GlideImageLoader())//加载器，*必须配置，目前内置的有GlideImageLoader或PicassoImageLoader，也可以自己实现
                        .theme(R.style.ImageViewerTheme)//设置主题风格，默认：R.style.ImageViewerTheme
                        .orientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)//设置屏幕方向,默认：ActivityInfo.SCREEN_ORIENTATION_BEHIND
                        .start(PhotoList3Activity.this, v);
            }
        });

    }
}